package com.techsun.json.format.defaultConfig.util;


import com.techsun.json.format.defaultConfig.data.constant.InternationalConstant;
import com.techsun.json.format.defaultConfig.data.enums.TimePatternEnum;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * 多语言格式化工具类
 * 该类的格式化pattern规范 请参考国际化文档
 * @author chenYX
 *
 * @date 2021/8/16 10:52
 * @description
 */
public class InternationalFormatUtils {
    /**
     * @param
     * @author : yongxiang
     * @description : format double数字
     * @date : 10:58 2021/8/16
     */
    public static String formatNumberStr(String pattern, String numberStr) {

        String result = numberStr;
        if (StringUtils.isNotBlank(pattern) && StringUtils.isNotBlank(numberStr)){
            String[] split = numberStr.split("\\.");
            String integerStr = split[0];
            integerStr = formatIntegerNumberStr(pattern,integerStr);
            result = integerStr;
            if (split.length > 1){
                //说明小数点
                result = result + "." + split[1];
            }
        }
        return result;
    }

    public static String formatIntegerNumberStr(String pattern, String integerNumberStr) {
        if (StringUtils.isNotBlank(pattern) && StringUtils.isNotBlank(integerNumberStr)){
            //数字的格式是 d3s, 表示每间隔3哥数字用,隔开
            //取出3 和 分隔符，
            int beginIndex = pattern.indexOf(InternationalConstant.NUMBER_SIGN);
            int endIndex = pattern.lastIndexOf(InternationalConstant.JOIN_SIGN);
            String spaceStr = pattern.substring(beginIndex + 1, endIndex);
            Integer spaceNum = Integer.valueOf(spaceStr);
            //取出分隔符
            String separator = pattern.substring(pattern.length()-1);
            char[] originalStr = integerNumberStr.toCharArray();
            StringBuilder strBuilder = new StringBuilder();
            int length = originalStr.length;
            for (int i = 0; i < length; i++) {
                strBuilder.append(originalStr[i]);
                if (((i+1) % spaceNum) == 0 && i != length-1){
                    strBuilder.append(separator);
                }
            }
            return strBuilder.toString();
        }
        return StringUtils.EMPTY;
    }

    /**
     *
     * @author : yongxiang
     * @description : format 时间
     * @param date 日期
     * @param timePattern 时间格式code
     * @date : 14:32 2021/8/16
    */
    public static String formatTime(Date date,String timePattern){
        //日期的格式为 24|am|pm
        if (date != null){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            if (StringUtils.isBlank(timePattern)){
                timePattern = TimePatternEnum.PATTERN_24.getTimePatternCode();
            }
            String[] timeStr = timePattern.split("\\|");
            String patternCode = timeStr[0];

            int hour = calendar.get(Calendar.HOUR_OF_DAY);
            //下午flag
            boolean pmFlag = hour > 12;
            boolean flagFor12 = StringUtils.equals(patternCode, TimePatternEnum.PATTERN_12.getTimePatternCode());
            if (pmFlag && flagFor12){
                //12小时制
                hour = hour - 12;
            }
            int minute = calendar.get(Calendar.MINUTE);
            String result = hour + ":" + minute;
            if (flagFor12){
                result += StringUtils.EMPTY;
                if (pmFlag){
                    //下午
                    result += timeStr[2];
                }else {
                    //上午
                    result += timeStr[1];
                }
            }
            return result;
        }
        return StringUtils.EMPTY;
    }


    /**
     *
     * @author : yongxiang
     * @description : 长时间
     * @param
     * @date : 14:03 2021/8/20
    */
    public static String formatLongDate(Date date,String longDatePattern){
        //星期的格式是  ww|,|yyyy/MM/dd|星期一|星期二|星期三。。。
        String result = StringUtils.EMPTY;
        if (date != null && StringUtils.isNotBlank(longDatePattern)) {
            //获取时间
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            //获取星期
            int weekIndex = calendar.get(Calendar.DAY_OF_WEEK);
            //获取星期的渲染方式
            String[] split = longDatePattern.split("\\|");
            //前两个表示的是星期或者日期格式
            String weekStr = StringUtils.EMPTY;
            weekStr = split[weekIndex + 2];
            String datePattern = null;
            //星期是否在前
            boolean weekWithFirstFlag = split[0].contains(InternationalConstant.weekSign);
            if (weekWithFirstFlag){
                datePattern = split[2];
            }else {
                datePattern = split[0];
            }
            //分隔符
            String separator  = split[1];

            String datesStr = DateFormatUtils.format(date, datePattern);
            if (weekWithFirstFlag){
                result = weekStr + separator + datesStr;
            }else {
                result = datesStr + separator + weekStr;
            }

        }
        return result;
    }

    /**
     *
     * @author : yongxiang
     * @description : 短日期
     * @param
     * @date : 9:37 2021/8/19
    */
    public static String formatShortDate(Date date,String pattern){
        if (date != null && pattern != null) {
            return DateFormatUtils.format(date, pattern);
        }
        return StringUtils.EMPTY;
    }





}
